package com.learn.smart.framework.util;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtil {
	
	public static String getJson(Object obj){
		ObjectMapper om = new ObjectMapper();
		String str = null;;
		try {
			str = om.writeValueAsString(obj);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T fromJSON(String json, Class<?> clazz) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.configure(Feature.AUTO_DETECT_SETTERS, true);
			return (T) mapper.readValue(json, clazz);
		} catch (JsonParseException e) {
			throw new RuntimeException(e);
		} catch (JsonMappingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}
